<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/layers/pooling_layer.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/layers</a> - pooling_layer.cpp<span style="font-size: 80%;"> (source / <a href="pooling_layer.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">92</td>
            <td class="headerCovTableEntry">173</td>
            <td class="headerCovTableEntryLo">53.2 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:50:33</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">5</td>
            <td class="headerCovTableEntry">13</td>
            <td class="headerCovTableEntryLo">38.5 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &lt;algorithm&gt;</a>
<span class="lineNum">       2 </span>            : #include &lt;cfloat&gt;
<span class="lineNum">       3 </span>            : #include &lt;vector&gt;
<span class="lineNum">       4 </span>            : 
<span class="lineNum">       5 </span>            : #include &quot;caffe/layers/pooling_layer.hpp&quot;
<span class="lineNum">       6 </span>            : #include &quot;caffe/util/math_functions.hpp&quot;
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : namespace caffe {
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span>            : using std::min;
<span class="lineNum">      11 </span>            : using std::max;
<a name="12"><span class="lineNum">      12 </span>            : </a>
<span class="lineNum">      13 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      14 </span><span class="lineCov">          4 : void PoolingLayer&lt;Dtype&gt;::LayerSetUp(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      15 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      16 </span><span class="lineCov">          8 :   PoolingParameter pool_param = this-&gt;layer_param_.pooling_param();</span>
<span class="lineNum">      17 </span><span class="lineCov">          4 :   if (pool_param.global_pooling()) {</span>
<span class="lineNum">      18 </span><span class="lineNoCov">          0 :     CHECK(!(pool_param.has_kernel_size() ||</span>
<span class="lineNum">      19 </span>            :       pool_param.has_kernel_h() || pool_param.has_kernel_w()))
<span class="lineNum">      20 </span>            :       &lt;&lt; &quot;With Global_pooling: true Filter size cannot specified&quot;;
<span class="lineNum">      21 </span>            :   } else {
<span class="lineNum">      22 </span><span class="lineCov">          8 :     CHECK(!pool_param.has_kernel_size() !=</span>
<span class="lineNum">      23 </span>            :       !(pool_param.has_kernel_h() &amp;&amp; pool_param.has_kernel_w()))
<span class="lineNum">      24 </span>            :       &lt;&lt; &quot;Filter size is kernel_size OR kernel_h and kernel_w; not both&quot;;
<span class="lineNum">      25 </span><span class="lineCov">          8 :     CHECK(pool_param.has_kernel_size() ||</span>
<span class="lineNum">      26 </span>            :       (pool_param.has_kernel_h() &amp;&amp; pool_param.has_kernel_w()))
<span class="lineNum">      27 </span>            :       &lt;&lt; &quot;For non-square filters both kernel_h and kernel_w are required.&quot;;
<span class="lineNum">      28 </span>            :   }
<span class="lineNum">      29 </span><span class="lineCov">         20 :   CHECK((!pool_param.has_pad() &amp;&amp; pool_param.has_pad_h()</span>
<span class="lineNum">      30 </span>            :       &amp;&amp; pool_param.has_pad_w())
<span class="lineNum">      31 </span>            :       || (!pool_param.has_pad_h() &amp;&amp; !pool_param.has_pad_w()))
<span class="lineNum">      32 </span>            :       &lt;&lt; &quot;pad is pad OR pad_h and pad_w are required.&quot;;
<span class="lineNum">      33 </span><span class="lineCov">         16 :   CHECK((!pool_param.has_stride() &amp;&amp; pool_param.has_stride_h()</span>
<span class="lineNum">      34 </span>            :       &amp;&amp; pool_param.has_stride_w())
<span class="lineNum">      35 </span>            :       || (!pool_param.has_stride_h() &amp;&amp; !pool_param.has_stride_w()))
<span class="lineNum">      36 </span>            :       &lt;&lt; &quot;Stride is stride OR stride_h and stride_w are required.&quot;;
<span class="lineNum">      37 </span><span class="lineCov">          4 :   global_pooling_ = pool_param.global_pooling();</span>
<span class="lineNum">      38 </span><span class="lineCov">          4 :   round_mode_ = pool_param.round_mode();</span>
<span class="lineNum">      39 </span><span class="lineCov">          4 :   if (global_pooling_) {</span>
<span class="lineNum">      40 </span><span class="lineNoCov">          0 :     kernel_h_ = bottom[0]-&gt;height();</span>
<span class="lineNum">      41 </span><span class="lineNoCov">          0 :     kernel_w_ = bottom[0]-&gt;width();</span>
<span class="lineNum">      42 </span>            :   } else {
<span class="lineNum">      43 </span><span class="lineCov">          4 :     if (pool_param.has_kernel_size()) {</span>
<span class="lineNum">      44 </span><span class="lineCov">          4 :       kernel_h_ = kernel_w_ = pool_param.kernel_size();</span>
<span class="lineNum">      45 </span>            :     } else {
<span class="lineNum">      46 </span><span class="lineNoCov">          0 :       kernel_h_ = pool_param.kernel_h();</span>
<span class="lineNum">      47 </span><span class="lineNoCov">          0 :       kernel_w_ = pool_param.kernel_w();</span>
<span class="lineNum">      48 </span>            :     }
<span class="lineNum">      49 </span>            :   }
<span class="lineNum">      50 </span><span class="lineCov">          8 :   CHECK_GT(kernel_h_, 0) &lt;&lt; &quot;Filter dimensions cannot be zero.&quot;;</span>
<span class="lineNum">      51 </span><span class="lineCov">          8 :   CHECK_GT(kernel_w_, 0) &lt;&lt; &quot;Filter dimensions cannot be zero.&quot;;</span>
<span class="lineNum">      52 </span><span class="lineCov">          4 :   if (!pool_param.has_pad_h()) {</span>
<span class="lineNum">      53 </span><span class="lineCov">          4 :     pad_h_ = pad_w_ = pool_param.pad();</span>
<span class="lineNum">      54 </span>            :   } else {
<span class="lineNum">      55 </span><span class="lineNoCov">          0 :     pad_h_ = pool_param.pad_h();</span>
<span class="lineNum">      56 </span><span class="lineNoCov">          0 :     pad_w_ = pool_param.pad_w();</span>
<span class="lineNum">      57 </span>            :   }
<span class="lineNum">      58 </span><span class="lineCov">          4 :   if (!pool_param.has_stride_h()) {</span>
<span class="lineNum">      59 </span><span class="lineCov">          4 :     stride_h_ = stride_w_ = pool_param.stride();</span>
<span class="lineNum">      60 </span>            :   } else {
<span class="lineNum">      61 </span><span class="lineNoCov">          0 :     stride_h_ = pool_param.stride_h();</span>
<span class="lineNum">      62 </span><span class="lineNoCov">          0 :     stride_w_ = pool_param.stride_w();</span>
<span class="lineNum">      63 </span>            :   }
<span class="lineNum">      64 </span><span class="lineCov">          4 :   if (global_pooling_) {</span>
<span class="lineNum">      65 </span><span class="lineNoCov">          0 :     CHECK(pad_h_ == 0 &amp;&amp; pad_w_ == 0 &amp;&amp; stride_h_ == 1 &amp;&amp; stride_w_ == 1)</span>
<span class="lineNum">      66 </span>            :       &lt;&lt; &quot;With Global_pooling: true; only pad = 0 and stride = 1&quot;;
<span class="lineNum">      67 </span>            :   }
<span class="lineNum">      68 </span><span class="lineCov">          4 :   if (pad_h_ != 0 || pad_w_ != 0) {</span>
<span class="lineNum">      69 </span><span class="lineNoCov">          0 :     CHECK(this-&gt;layer_param_.pooling_param().pool()</span>
<span class="lineNum">      70 </span>            :         == PoolingParameter_PoolMethod_AVE
<span class="lineNum">      71 </span>            :         || this-&gt;layer_param_.pooling_param().pool()
<span class="lineNum">      72 </span>            :         == PoolingParameter_PoolMethod_MAX)
<span class="lineNum">      73 </span>            :         &lt;&lt; &quot;Padding implemented only for average and max pooling.&quot;;
<span class="lineNum">      74 </span><span class="lineNoCov">          0 :     CHECK_LT(pad_h_, kernel_h_);</span>
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :     CHECK_LT(pad_w_, kernel_w_);</span>
<span class="lineNum">      76 </span>            :   }
<span class="lineNum">      77 </span><span class="lineCov">          4 : }</span>
<a name="78"><span class="lineNum">      78 </span>            : </a>
<span class="lineNum">      79 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      80 </span><span class="lineCov">      24206 : void PoolingLayer&lt;Dtype&gt;::Reshape(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      81 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      82 </span><span class="lineCov">      48412 :   CHECK_EQ(4, bottom[0]-&gt;num_axes()) &lt;&lt; &quot;Input must have 4 axes, &quot;</span>
<span class="lineNum">      83 </span>            :       &lt;&lt; &quot;corresponding to (num, channels, height, width)&quot;;
<span class="lineNum">      84 </span><span class="lineCov">      48412 :   channels_ = bottom[0]-&gt;channels();</span>
<span class="lineNum">      85 </span><span class="lineCov">      48412 :   height_ = bottom[0]-&gt;height();</span>
<span class="lineNum">      86 </span><span class="lineCov">      48412 :   width_ = bottom[0]-&gt;width();</span>
<span class="lineNum">      87 </span><span class="lineCov">      24206 :   if (global_pooling_) {</span>
<span class="lineNum">      88 </span><span class="lineNoCov">          0 :     kernel_h_ = bottom[0]-&gt;height();</span>
<span class="lineNum">      89 </span><span class="lineNoCov">          0 :     kernel_w_ = bottom[0]-&gt;width();</span>
<span class="lineNum">      90 </span>            :   }
<span class="lineNum">      91 </span><span class="lineCov">      24206 :   switch (round_mode_) {</span>
<span class="lineNum">      92 </span>            :   case PoolingParameter_RoundMode_CEIL:
<span class="lineNum">      93 </span><span class="lineCov">      72618 :     pooled_height_ = static_cast&lt;int&gt;(ceil(static_cast&lt;float&gt;(</span>
<span class="lineNum">      94 </span><span class="lineCov">      72618 :         height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1;</span>
<span class="lineNum">      95 </span><span class="lineCov">      72618 :     pooled_width_ = static_cast&lt;int&gt;(ceil(static_cast&lt;float&gt;(</span>
<span class="lineNum">      96 </span><span class="lineCov">      72618 :         width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1;</span>
<span class="lineNum">      97 </span><span class="lineCov">      24206 :     break;</span>
<span class="lineNum">      98 </span>            :   case PoolingParameter_RoundMode_FLOOR:
<span class="lineNum">      99 </span><span class="lineNoCov">          0 :     pooled_height_ = static_cast&lt;int&gt;(floor(static_cast&lt;float&gt;(</span>
<span class="lineNum">     100 </span><span class="lineNoCov">          0 :         height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1;</span>
<span class="lineNum">     101 </span><span class="lineNoCov">          0 :     pooled_width_ = static_cast&lt;int&gt;(floor(static_cast&lt;float&gt;(</span>
<span class="lineNum">     102 </span><span class="lineNoCov">          0 :         width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1;</span>
<span class="lineNum">     103 </span><span class="lineNoCov">          0 :     break;</span>
<span class="lineNum">     104 </span>            :   default:
<span class="lineNum">     105 </span><span class="lineNoCov">          0 :     LOG(FATAL) &lt;&lt; &quot;Unknown rounding mode.&quot;;</span>
<span class="lineNum">     106 </span>            :   }
<span class="lineNum">     107 </span><span class="lineCov">      24206 :   if (pad_h_ || pad_w_) {</span>
<span class="lineNum">     108 </span>            :     // If we have padding, ensure that the last pooling starts strictly
<span class="lineNum">     109 </span>            :     // inside the image (instead of at the padding); otherwise clip the last.
<span class="lineNum">     110 </span><span class="lineNoCov">          0 :     if ((pooled_height_ - 1) * stride_h_ &gt;= height_ + pad_h_) {</span>
<span class="lineNum">     111 </span><span class="lineNoCov">          0 :       --pooled_height_;</span>
<span class="lineNum">     112 </span>            :     }
<span class="lineNum">     113 </span><span class="lineNoCov">          0 :     if ((pooled_width_ - 1) * stride_w_ &gt;= width_ + pad_w_) {</span>
<span class="lineNum">     114 </span><span class="lineNoCov">          0 :       --pooled_width_;</span>
<span class="lineNum">     115 </span>            :     }
<span class="lineNum">     116 </span><span class="lineNoCov">          0 :     CHECK_LT((pooled_height_ - 1) * stride_h_, height_ + pad_h_);</span>
<span class="lineNum">     117 </span><span class="lineNoCov">          0 :     CHECK_LT((pooled_width_ - 1) * stride_w_, width_ + pad_w_);</span>
<span class="lineNum">     118 </span>            :   }
<span class="lineNum">     119 </span><span class="lineCov">      48412 :   top[0]-&gt;Reshape(bottom[0]-&gt;num(), channels_, pooled_height_,</span>
<span class="lineNum">     120 </span>            :       pooled_width_);
<span class="lineNum">     121 </span><span class="lineCov">      24206 :   if (top.size() &gt; 1) {</span>
<span class="lineNum">     122 </span><span class="lineNoCov">          0 :     top[1]-&gt;ReshapeLike(*top[0]);</span>
<span class="lineNum">     123 </span>            :   }
<span class="lineNum">     124 </span>            :   // If max pooling, we will initialize the vector index part.
<span class="lineNum">     125 </span><span class="lineCov">      48412 :   if (this-&gt;layer_param_.pooling_param().pool() ==</span>
<span class="lineNum">     126 </span>            :       PoolingParameter_PoolMethod_MAX &amp;&amp; top.size() == 1) {
<span class="lineNum">     127 </span><span class="lineCov">      48412 :     max_idx_.Reshape(bottom[0]-&gt;num(), channels_, pooled_height_,</span>
<span class="lineNum">     128 </span>            :         pooled_width_);
<span class="lineNum">     129 </span>            :   }
<span class="lineNum">     130 </span>            :   // If stochastic pooling, we will initialize the random index part.
<span class="lineNum">     131 </span><span class="lineCov">      24206 :   if (this-&gt;layer_param_.pooling_param().pool() ==</span>
<span class="lineNum">     132 </span>            :       PoolingParameter_PoolMethod_STOCHASTIC) {
<span class="lineNum">     133 </span><span class="lineNoCov">          0 :     rand_idx_.Reshape(bottom[0]-&gt;num(), channels_, pooled_height_,</span>
<span class="lineNum">     134 </span>            :       pooled_width_);
<span class="lineNum">     135 </span>            :   }
<span class="lineNum">     136 </span><span class="lineCov">      24206 : }</span>
<span class="lineNum">     137 </span>            : 
<span class="lineNum">     138 </span>            : // TODO(Yangqing): Is there a faster way to do pooling in the channel-first
<a name="139"><span class="lineNum">     139 </span>            : // case?</a>
<span class="lineNum">     140 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     141 </span><span class="lineCov">      24202 : void PoolingLayer&lt;Dtype&gt;::Forward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">     142 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">     143 </span><span class="lineCov">      24202 :   const Dtype* bottom_data = bottom[0]-&gt;cpu_data();</span>
<span class="lineNum">     144 </span><span class="lineCov">      24202 :   Dtype* top_data = top[0]-&gt;mutable_cpu_data();</span>
<span class="lineNum">     145 </span><span class="lineCov">      24202 :   const int top_count = top[0]-&gt;count();</span>
<span class="lineNum">     146 </span>            :   // We'll output the mask to top[1] if it's of size &gt;1.
<span class="lineNum">     147 </span>            :   const bool use_top_mask = top.size() &gt; 1;
<span class="lineNum">     148 </span>            :   int* mask = NULL;  // suppress warnings about uninitialized variables
<span class="lineNum">     149 </span>            :   Dtype* top_mask = NULL;
<span class="lineNum">     150 </span>            :   // Different pooling methods. We explicitly do the switch outside the for
<span class="lineNum">     151 </span>            :   // loop to save time, although this results in more code.
<span class="lineNum">     152 </span><span class="lineCov">      24202 :   switch (this-&gt;layer_param_.pooling_param().pool()) {</span>
<span class="lineNum">     153 </span>            :   case PoolingParameter_PoolMethod_MAX:
<span class="lineNum">     154 </span>            :     // Initialize
<span class="lineNum">     155 </span><span class="lineCov">      24202 :     if (use_top_mask) {</span>
<span class="lineNum">     156 </span><span class="lineNoCov">          0 :       top_mask = top[1]-&gt;mutable_cpu_data();</span>
<span class="lineNum">     157 </span><span class="lineNoCov">          0 :       caffe_set(top_count, Dtype(-1), top_mask);</span>
<span class="lineNum">     158 </span>            :     } else {
<span class="lineNum">     159 </span><span class="lineCov">      24202 :       mask = max_idx_.mutable_cpu_data();</span>
<span class="lineNum">     160 </span><span class="lineCov">      24202 :       caffe_set(top_count, -1, mask);</span>
<span class="lineNum">     161 </span>            :     }
<span class="lineNum">     162 </span><span class="lineCov">      24202 :     caffe_set(top_count, Dtype(-FLT_MAX), top_data);</span>
<span class="lineNum">     163 </span>            :     // The main loop
<span class="lineNum">     164 </span><span class="lineCov">    5148788 :     for (int n = 0; n &lt; bottom[0]-&gt;num(); ++n) {</span>
<span class="lineNum">     165 </span><span class="lineCov">  120709088 :       for (int c = 0; c &lt; channels_; ++c) {</span>
<span class="lineNum">     166 </span><span class="lineCov">  807560800 :         for (int ph = 0; ph &lt; pooled_height_; ++ph) {</span>
<span class="lineNum">     167 </span><span class="lineCov"> 6630499200 :           for (int pw = 0; pw &lt; pooled_width_; ++pw) {</span>
<span class="lineNum">     168 </span><span class="lineCov"> 3128235520 :             int hstart = ph * stride_h_ - pad_h_;</span>
<span class="lineNum">     169 </span><span class="lineCov"> 3128235520 :             int wstart = pw * stride_w_ - pad_w_;</span>
<span class="lineNum">     170 </span><span class="lineCov"> 6256471040 :             int hend = min(hstart + kernel_h_, height_);</span>
<span class="lineNum">     171 </span><span class="lineCov"> 6256471040 :             int wend = min(wstart + kernel_w_, width_);</span>
<span class="lineNum">     172 </span><span class="lineCov"> 6256471040 :             hstart = max(hstart, 0);</span>
<span class="lineNum">     173 </span><span class="lineCov"> 6256471040 :             wstart = max(wstart, 0);</span>
<span class="lineNum">     174 </span><span class="lineCov"> 3128235520 :             const int pool_index = ph * pooled_width_ + pw;</span>
<span class="lineNum">     175 </span><span class="lineCov">15641177600 :             for (int h = hstart; h &lt; hend; ++h) {</span>
<span class="lineNum">     176 </span><span class="lineCov">31282355200 :               for (int w = wstart; w &lt; wend; ++w) {</span>
<span class="lineNum">     177 </span><span class="lineCov">12512942080 :                 const int index = h * width_ + w;</span>
<span class="lineNum">     178 </span><span class="lineCov">12512942080 :                 if (bottom_data[index] &gt; top_data[pool_index]) {</span>
<span class="lineNum">     179 </span><span class="lineCov"> 5590612933 :                   top_data[pool_index] = bottom_data[index];</span>
<span class="lineNum">     180 </span><span class="lineCov"> 5590612933 :                   if (use_top_mask) {</span>
<span class="lineNum">     181 </span><span class="lineNoCov">          0 :                     top_mask[pool_index] = static_cast&lt;Dtype&gt;(index);</span>
<span class="lineNum">     182 </span>            :                   } else {
<span class="lineNum">     183 </span><span class="lineCov"> 5590612933 :                     mask[pool_index] = index;</span>
<span class="lineNum">     184 </span>            :                   }
<span class="lineNum">     185 </span>            :                 }
<span class="lineNum">     186 </span>            :               }
<span class="lineNum">     187 </span>            :             }
<span class="lineNum">     188 </span>            :           }
<span class="lineNum">     189 </span>            :         }
<span class="lineNum">     190 </span>            :         // compute offset
<span class="lineNum">     191 </span><span class="lineCov">   59504480 :         bottom_data += bottom[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     192 </span><span class="lineCov">   59504480 :         top_data += top[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     193 </span><span class="lineCov">   59504480 :         if (use_top_mask) {</span>
<span class="lineNum">     194 </span><span class="lineNoCov">          0 :           top_mask += top[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     195 </span>            :         } else {
<span class="lineNum">     196 </span><span class="lineCov">   59504480 :           mask += top[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     197 </span>            :         }
<span class="lineNum">     198 </span>            :       }
<span class="lineNum">     199 </span>            :     }
<span class="lineNum">     200 </span>            :     break;
<span class="lineNum">     201 </span>            :   case PoolingParameter_PoolMethod_AVE:
<span class="lineNum">     202 </span><span class="lineNoCov">          0 :     for (int i = 0; i &lt; top_count; ++i) {</span>
<span class="lineNum">     203 </span><span class="lineNoCov">          0 :       top_data[i] = 0;</span>
<span class="lineNum">     204 </span>            :     }
<span class="lineNum">     205 </span>            :     // The main loop
<span class="lineNum">     206 </span><span class="lineNoCov">          0 :     for (int n = 0; n &lt; bottom[0]-&gt;num(); ++n) {</span>
<span class="lineNum">     207 </span><span class="lineNoCov">          0 :       for (int c = 0; c &lt; channels_; ++c) {</span>
<span class="lineNum">     208 </span><span class="lineNoCov">          0 :         for (int ph = 0; ph &lt; pooled_height_; ++ph) {</span>
<span class="lineNum">     209 </span><span class="lineNoCov">          0 :           for (int pw = 0; pw &lt; pooled_width_; ++pw) {</span>
<span class="lineNum">     210 </span><span class="lineNoCov">          0 :             int hstart = ph * stride_h_ - pad_h_;</span>
<span class="lineNum">     211 </span><span class="lineNoCov">          0 :             int wstart = pw * stride_w_ - pad_w_;</span>
<span class="lineNum">     212 </span><span class="lineNoCov">          0 :             int hend = min(hstart + kernel_h_, height_ + pad_h_);</span>
<span class="lineNum">     213 </span><span class="lineNoCov">          0 :             int wend = min(wstart + kernel_w_, width_ + pad_w_);</span>
<span class="lineNum">     214 </span><span class="lineNoCov">          0 :             int pool_size = (hend - hstart) * (wend - wstart);</span>
<span class="lineNum">     215 </span><span class="lineNoCov">          0 :             hstart = max(hstart, 0);</span>
<span class="lineNum">     216 </span><span class="lineNoCov">          0 :             wstart = max(wstart, 0);</span>
<span class="lineNum">     217 </span><span class="lineNoCov">          0 :             hend = min(hend, height_);</span>
<span class="lineNum">     218 </span><span class="lineNoCov">          0 :             wend = min(wend, width_);</span>
<span class="lineNum">     219 </span><span class="lineNoCov">          0 :             for (int h = hstart; h &lt; hend; ++h) {</span>
<span class="lineNum">     220 </span><span class="lineNoCov">          0 :               for (int w = wstart; w &lt; wend; ++w) {</span>
<span class="lineNum">     221 </span><span class="lineNoCov">          0 :                 top_data[ph * pooled_width_ + pw] +=</span>
<span class="lineNum">     222 </span><span class="lineNoCov">          0 :                     bottom_data[h * width_ + w];</span>
<span class="lineNum">     223 </span>            :               }
<span class="lineNum">     224 </span>            :             }
<span class="lineNum">     225 </span><span class="lineNoCov">          0 :             top_data[ph * pooled_width_ + pw] /= pool_size;</span>
<span class="lineNum">     226 </span>            :           }
<span class="lineNum">     227 </span>            :         }
<span class="lineNum">     228 </span>            :         // compute offset
<span class="lineNum">     229 </span><span class="lineNoCov">          0 :         bottom_data += bottom[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     230 </span><span class="lineNoCov">          0 :         top_data += top[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     231 </span>            :       }
<span class="lineNum">     232 </span>            :     }
<span class="lineNum">     233 </span>            :     break;
<span class="lineNum">     234 </span>            :   case PoolingParameter_PoolMethod_STOCHASTIC:
<span class="lineNum">     235 </span><span class="lineNoCov">          0 :     NOT_IMPLEMENTED;</span>
<span class="lineNum">     236 </span>            :     break;
<span class="lineNum">     237 </span>            :   default:
<span class="lineNum">     238 </span><span class="lineNoCov">          0 :     LOG(FATAL) &lt;&lt; &quot;Unknown pooling method.&quot;;</span>
<span class="lineNum">     239 </span>            :   }
<span class="lineNum">     240 </span><span class="lineCov">      24202 : }</span>
<a name="241"><span class="lineNum">     241 </span>            : </a>
<span class="lineNum">     242 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     243 </span><span class="lineCov">      20000 : void PoolingLayer&lt;Dtype&gt;::Backward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top,</span>
<span class="lineNum">     244 </span>            :       const vector&lt;bool&gt;&amp; propagate_down, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom) {
<span class="lineNum">     245 </span><span class="lineCov">      20000 :   if (!propagate_down[0]) {</span>
<span class="lineNum">     246 </span>            :     return;
<span class="lineNum">     247 </span>            :   }
<span class="lineNum">     248 </span><span class="lineCov">      20000 :   const Dtype* top_diff = top[0]-&gt;cpu_diff();</span>
<span class="lineNum">     249 </span><span class="lineCov">      20000 :   Dtype* bottom_diff = bottom[0]-&gt;mutable_cpu_diff();</span>
<span class="lineNum">     250 </span>            :   // Different pooling methods. We explicitly do the switch outside the for
<span class="lineNum">     251 </span>            :   // loop to save time, although this results in more codes.
<span class="lineNum">     252 </span><span class="lineCov">      20000 :   caffe_set(bottom[0]-&gt;count(), Dtype(0), bottom_diff);</span>
<span class="lineNum">     253 </span>            :   // We'll output the mask to top[1] if it's of size &gt;1.
<span class="lineNum">     254 </span>            :   const bool use_top_mask = top.size() &gt; 1;
<span class="lineNum">     255 </span>            :   const int* mask = NULL;  // suppress warnings about uninitialized variables
<span class="lineNum">     256 </span>            :   const Dtype* top_mask = NULL;
<span class="lineNum">     257 </span><span class="lineCov">      20000 :   switch (this-&gt;layer_param_.pooling_param().pool()) {</span>
<span class="lineNum">     258 </span>            :   case PoolingParameter_PoolMethod_MAX:
<span class="lineNum">     259 </span>            :     // The main loop
<span class="lineNum">     260 </span><span class="lineCov">      20000 :     if (use_top_mask) {</span>
<span class="lineNum">     261 </span><span class="lineNoCov">          0 :       top_mask = top[1]-&gt;cpu_data();</span>
<span class="lineNum">     262 </span>            :     } else {
<span class="lineNum">     263 </span><span class="lineCov">      20000 :       mask = max_idx_.cpu_data();</span>
<span class="lineNum">     264 </span>            :     }
<span class="lineNum">     265 </span><span class="lineCov">    3880000 :     for (int n = 0; n &lt; top[0]-&gt;num(); ++n) {</span>
<span class="lineNum">     266 </span><span class="lineCov">   90880000 :       for (int c = 0; c &lt; channels_; ++c) {</span>
<span class="lineNum">     267 </span><span class="lineCov">  608000000 :         for (int ph = 0; ph &lt; pooled_height_; ++ph) {</span>
<span class="lineNum">     268 </span><span class="lineCov"> 4992000000 :           for (int pw = 0; pw &lt; pooled_width_; ++pw) {</span>
<span class="lineNum">     269 </span><span class="lineCov"> 2355200000 :             const int index = ph * pooled_width_ + pw;</span>
<span class="lineNum">     270 </span>            :             const int bottom_index =
<span class="lineNum">     271 </span><span class="lineCov"> 2355200000 :                 use_top_mask ? top_mask[index] : mask[index];</span>
<span class="lineNum">     272 </span><span class="lineCov"> 2355200000 :             bottom_diff[bottom_index] += top_diff[index];</span>
<span class="lineNum">     273 </span>            :           }
<span class="lineNum">     274 </span>            :         }
<span class="lineNum">     275 </span><span class="lineCov">   44800000 :         bottom_diff += bottom[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     276 </span><span class="lineCov">   44800000 :         top_diff += top[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     277 </span><span class="lineCov">   44800000 :         if (use_top_mask) {</span>
<span class="lineNum">     278 </span><span class="lineNoCov">          0 :           top_mask += top[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     279 </span>            :         } else {
<span class="lineNum">     280 </span><span class="lineCov">   44800000 :           mask += top[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     281 </span>            :         }
<span class="lineNum">     282 </span>            :       }
<span class="lineNum">     283 </span>            :     }
<span class="lineNum">     284 </span>            :     break;
<span class="lineNum">     285 </span>            :   case PoolingParameter_PoolMethod_AVE:
<span class="lineNum">     286 </span>            :     // The main loop
<span class="lineNum">     287 </span><span class="lineNoCov">          0 :     for (int n = 0; n &lt; top[0]-&gt;num(); ++n) {</span>
<span class="lineNum">     288 </span><span class="lineNoCov">          0 :       for (int c = 0; c &lt; channels_; ++c) {</span>
<span class="lineNum">     289 </span><span class="lineNoCov">          0 :         for (int ph = 0; ph &lt; pooled_height_; ++ph) {</span>
<span class="lineNum">     290 </span><span class="lineNoCov">          0 :           for (int pw = 0; pw &lt; pooled_width_; ++pw) {</span>
<span class="lineNum">     291 </span><span class="lineNoCov">          0 :             int hstart = ph * stride_h_ - pad_h_;</span>
<span class="lineNum">     292 </span><span class="lineNoCov">          0 :             int wstart = pw * stride_w_ - pad_w_;</span>
<span class="lineNum">     293 </span><span class="lineNoCov">          0 :             int hend = min(hstart + kernel_h_, height_ + pad_h_);</span>
<span class="lineNum">     294 </span><span class="lineNoCov">          0 :             int wend = min(wstart + kernel_w_, width_ + pad_w_);</span>
<span class="lineNum">     295 </span><span class="lineNoCov">          0 :             int pool_size = (hend - hstart) * (wend - wstart);</span>
<span class="lineNum">     296 </span><span class="lineNoCov">          0 :             hstart = max(hstart, 0);</span>
<span class="lineNum">     297 </span><span class="lineNoCov">          0 :             wstart = max(wstart, 0);</span>
<span class="lineNum">     298 </span><span class="lineNoCov">          0 :             hend = min(hend, height_);</span>
<span class="lineNum">     299 </span><span class="lineNoCov">          0 :             wend = min(wend, width_);</span>
<span class="lineNum">     300 </span><span class="lineNoCov">          0 :             for (int h = hstart; h &lt; hend; ++h) {</span>
<span class="lineNum">     301 </span><span class="lineNoCov">          0 :               for (int w = wstart; w &lt; wend; ++w) {</span>
<span class="lineNum">     302 </span><span class="lineNoCov">          0 :                 bottom_diff[h * width_ + w] +=</span>
<span class="lineNum">     303 </span><span class="lineNoCov">          0 :                   top_diff[ph * pooled_width_ + pw] / pool_size;</span>
<span class="lineNum">     304 </span>            :               }
<span class="lineNum">     305 </span>            :             }
<span class="lineNum">     306 </span>            :           }
<span class="lineNum">     307 </span>            :         }
<span class="lineNum">     308 </span>            :         // offset
<span class="lineNum">     309 </span><span class="lineNoCov">          0 :         bottom_diff += bottom[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     310 </span><span class="lineNoCov">          0 :         top_diff += top[0]-&gt;offset(0, 1);</span>
<span class="lineNum">     311 </span>            :       }
<span class="lineNum">     312 </span>            :     }
<span class="lineNum">     313 </span>            :     break;
<span class="lineNum">     314 </span>            :   case PoolingParameter_PoolMethod_STOCHASTIC:
<span class="lineNum">     315 </span><span class="lineNoCov">          0 :     NOT_IMPLEMENTED;</span>
<span class="lineNum">     316 </span>            :     break;
<span class="lineNum">     317 </span>            :   default:
<span class="lineNum">     318 </span><span class="lineNoCov">          0 :     LOG(FATAL) &lt;&lt; &quot;Unknown pooling method.&quot;;</span>
<span class="lineNum">     319 </span>            :   }
<span class="lineNum">     320 </span>            : }
<span class="lineNum">     321 </span>            : 
<a name="322"><span class="lineNum">     322 </span>            : </a>
<span class="lineNum">     323 </span>            : #ifdef CPU_ONLY
<span class="lineNum">     324 </span><span class="lineNoCov">          0 : STUB_GPU(PoolingLayer);</span>
<span class="lineNum">     325 </span>            : #endif
<span class="lineNum">     326 </span>            : 
<a name="327"><span class="lineNum">     327 </span>            : INSTANTIATE_CLASS(PoolingLayer);</a>
<span class="lineNum">     328 </span>            : 
<span class="lineNum">     329 </span><span class="lineCov">          2 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
